<template>
  <div class="dashboard-container">
    <div class="app-container">
      <div class="edit-form">
        <el-form
          ref="userFormRef"
          label-width="220px"
          :model="userForm"
          :rules="userRules"
        >
          <!-- 姓名 部门 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名" prop="username">
                <el-input
                  size="mini"
                  class="inputW"
                  v-model="userForm.username"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 工号 入职时间 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="工号" prop="workNumber">
                <el-input
                  disabled
                  size="mini"
                  class="inputW"
                  v-model="userForm.workNumber"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!--手机 聘用形式  -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="手机" prop="mobile">
                <el-input
                  :disabled="!!$route.params.id"
                  size="mini"
                  class="inputW"
                  v-model="userForm.mobile"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="部门" prop="departmentId">
                <!-- 放置及联部门组件 -->
                <!-- v-model：组件双向绑定的数据：选中的所有节点的value值组成的数组 -->
                <!-- options：下拉列表数据 -->
                <!-- props：修改组件默认读取字段 -->
                <el-cascader
                  v-model="userForm.departmentId"
                  :options="list"
                  :props="defaultProps"
                  class="inputW"
                  size="mini"
                  separator="-"
                ></el-cascader>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="聘用形式" prop="formOfEmployment">
                <el-select
                  size="mini"
                  class="inputW"
                  v-model="userForm.formOfEmployment"
                >
                  <el-option label="正式" :value="1"></el-option>
                  <el-option label="非正式" :value="2"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="入职时间" prop="timeOfEntry">
                <el-date-picker
                  v-model="userForm.timeOfEntry"
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="转正时间" prop="correctionTime">
                <el-date-picker
                  v-model="userForm.correctionTime"
                  size="mini"
                  type="date"
                  value-format="yyyy-MM-dd"
                  class="inputW"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 员工照片 -->
          <el-row>
            <el-col :span="12">
              <el-form-item label="员工头像">
                <!-- v-model="xxx" -->
                <!-- 会转换为： -->
                <!-- :value="xxx" @input="xxx = $event" -->
                <!-- 放置上传图片 -->
                <!-- <image-upload
                  :value="userForm.staffPhoto"
                  @input="userForm.staffPhoto = $event"
                ></image-upload> -->
                <!-- 等价于 -->
                <image-upload v-model="userForm.staffPhoto"></image-upload>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 保存个人信息 -->
          <el-row type="flex">
            <el-col :span="12" style="margin-left: 220px">
              <el-button size="mini" type="primary" @click="btnOK"
                >保存更新</el-button
              >
            </el-col>
          </el-row>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { getDepartmentList } from "@/api/department";
import { listToTree } from "@/utils/index";
import ImageUpload from "./components/image-upload.vue";
import { addEmployee, getEmployeeDetail, editEmpolyee } from "@/api/employee";
export default {
  components: {
    ImageUpload,
  },
  data() {
    return {
      userForm: {
        username: "",
        workNumber: "",
        mobile: "",
        departmentId: "",
        formOfEmployment: "",
        timeOfEntry: "",
        correctionTime: "",
        staffPhoto: "",
      },
      userRules: {
        username: [
          { required: true, message: "请输入姓名", trigger: "blur" },
          { min: 1, max: 4, message: "请输入1-4位字符", trigger: "blur" },
        ],
        mobile: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: "请输入正确格式的手机号",
            trigger: "blur",
          },
        ],
        departmentId: [
          { required: true, message: "请选择部门", trigger: "blur" },
        ],
        formOfEmployment: [
          { required: true, message: "请选择聘用形式", trigger: "blur" },
        ],
        timeOfEntry: [
          { required: true, message: "请选择入职时间", trigger: "blur" },
        ],
        correctionTime: [
          { required: true, message: "请选择转正时间", trigger: "blur" },
          // 转正时间不能小于入职时间
          {
            validator: (rule, value, cb) => {
              // value：转正时间（年月日形式的字符串）
              // this.userForm.timeOfEntry：入职时间（年月日形式的字符串）
              if (new Date(value) < new Date(this.userForm.timeOfEntry)) {
                // 校验不通过
                cb(new Error("转正时间不能小于入职时间"));
              } else {
                // 校验通过
                cb();
              }
            },
            trigger: "blur",
          },
        ],
      },
      // 级联选择器的下拉数据
      list: [],
      // 修改组件默认读取字段
      defaultProps: {
        value: "id",
        label: "name",
      },
    };
  },
  created() {
    // 获取部门列表数据
    this.getDepartmentList();
    // 获取员工详情数据
    if (this.$route.params.id) {
      this.getEmployeeDetail();
    }
  },
  methods: {
    // 获取部门列表数据
    async getDepartmentList() {
      const res = await getDepartmentList();
      this.list = listToTree(res.data, 0);
      console.log(this.list);
    },
    // 点击保存更新按钮
    btnOK() {
      // 兜底校验
      this.$refs.userFormRef.validate(async (isOK) => {
        if (isOK) {
          // 校验通过
          if (this.$route.params.id) {
            // 编辑
            await editEmpolyee(this.userForm);
            // 提示用户
            this.$message.success("编辑成功");
          } else {
            // 添加
            // 调用添加员工接口
            await addEmployee({
              ...this.userForm,
              departmentId:
                this.userForm.departmentId[
                  this.userForm.departmentId.length - 1
                ],
            });
            // 提示用户
            this.$message.success("添加成功");
          }
          // 跳转到员工列表页
          this.$router.push("/employee");
        }
      });
    },
    // 获取员工详情数据
    async getEmployeeDetail() {
      const res = await getEmployeeDetail(this.$route.params.id);
      // console.log(res);
      this.userForm = res.data;
    },
  },
};
</script>

<style scoped lang="scss">
.edit-form {
  background: #fff;
  padding: 20px;
  .inputW {
    width: 380px;
  }
}
</style>
